<?php
namespace Home\Controller;
/**
 * 用于外部调用
 * iframe方式内嵌播放
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class VideoPlayController extends HomeController {

    public function _initialize() {
        parent::_initialize();
        C('DEFAULT_THEME', 'player');
    }

    public function index()
    {
        // iframe播放改用jwplayer liubo 2016-8-13
        return $this->play();

        $vid = I('id', 0, 'intval');
        $videoEvent = event('video');
        $video = $videoEvent->info($vid);
        if (!isset($video['uri']) || empty($video['uri'])) {
            exit('参数有误');
        }
        if ($video['dpi_videos'] && $video['dpi_videos']['720'] && $video['dpi_videos']['720']['status'] == 'ok') {
            $video['uri'] = $video['dpi_videos']['720']['uri'];
        }
        uri_format($video['uri'], C('VIDEO_UPLOAD.uri_path'));
        uri_format($video['thumb']);
        $this->video = $video;

        $this->appid = I('appid');
        if (IS_MOBILE) {
            $this->display('mobile');
        } else {
            $this->display('pc');
        }
    }

    public function play() {
        C('HTML_FILE_SUFFIX', '.html');
        $vid = I('id', 0, 'intval');
        if (!$vid) {
            exit('参数错误');
        }
        $videoEvent = event('video');
        $video = $videoEvent->detail($vid);

        if (empty($video) || $video['status'] == -1) {
            exit('参数有误');
        }

        if ($video['type'] == 1) {
            $this->video = $video;
            $this->appid = I('appid');
            if (IS_MOBILE) {
                // $this->buildHtml('match-mobile-play-'.$vid, APP_PATH.'/../video/', 'match_mobile', 'utf8');
                return $this->display('match_mobile');
            }
            return $this->display('match_pc');
        }

        uri_format($video['uri'], C('VIDEO_UPLOAD.uri_path'));
        uri_format($video['thumb']);
        $this->video = $video;
        $this->autoplay = I('autoplay', 0, 'intval');

        $this->appid = I('appid');
        if (IS_MOBILE) {
            // 普通视频
            $playlist = [];
            $playlist[] = [
                'image' => $video['thumb'],
                'file' => $video['uri']
            ];
            $this->playlist = $playlist;
            // $this->buildHtml('play-'.$vid, APP_PATH.'/../video/', 'video_pc', 'utf8');
            $this->display('video_mobile');
        } else {
            
            $this->display('video_pc');
        }
    }


    public function htmlVideoPlay()
    {
        C('HTML_FILE_SUFFIX', '.html');
        $vid = I('id', 0, 'intval');
        $is_mobile = I('is_mobile', 0);
        if (!$vid) {
            exit('参数错误');
        }
        $videoEvent = event('video');
        $video = $videoEvent->detail($vid);

        if (empty($video)) {
            exit('参数有误');
        }

        if ($video['type'] == 1) {

            $this->video = $video;
            $this->appid = I('appid');
            if ($is_mobile) {
                $this->buildHtml('match-mobile-play-'.$vid, APP_PATH.'../video/', 'match_mobile', 'utf8');
                return $this->display('match_mobile');
            }
            $this->buildHtml('match-play-'.$vid, APP_PATH.'../video/', 'match_mobile', 'utf8');
            return $this->display('match_pc');
        }

        uri_format($video['uri'], C('VIDEO_UPLOAD.uri_path'));
        uri_format($video['thumb']);
        $this->video = $video;
        $this->autoplay = I('autoplay', 0, 'intval');

        $this->appid = I('appid');
        if ($is_mobile) {
            // 普通视频
            $playlist = [];
            $playlist[] = [
                'image' => $video['thumb'],
                'file' => $video['uri']
            ];
            $this->playlist = $playlist;
            $this->buildHtml('play-mobile-'.$vid, APP_PATH.'../video/', 'video_mobile', 'utf8');
            $this->display('video_mobile');
        } else {
            $this->buildHtml('play-'.$vid, APP_PATH.'../video/', 'video_pc', 'utf8');
            $this->display('video_pc');
        }
    }



    public function iframe() {
        $vid = I('id', 0, 'intval');
        if (!$vid) {
            exit('参数错误');
        }
        $videoEvent = event('video');
        $video = $videoEvent->detail($vid);

        if (empty($video)) {
            exit('参数有误');
        }
        uri_format($video['uri'], C('VIDEO_UPLOAD.uri_path'));
        uri_format($video['thumb']);
        $this->video = $video;
        $this->appid = I('appid');

        $this->display('iframe_mobile');
    }

    public function recommend() {
        $game_id = I('game_id', 0, 'intval');
        $limit = I('limit', 4, 'intval');
        if (!$game_id) {
            $this->ajaxReturn(['state' => -2, 'info' => '参数错误']);
        }
        $cache_key = 'IMBA_VIDEOS_RECOMMEND_GAME_'.$game_id.'_LIMIT_'.$limit;
        $videos = S($cache_key);
        if (!$videos){
            $vEvent = event('video');
            $map = [
                'game_id' => $game_id,
            ];
            $videos = $vEvent->lists($map, $limit);
            $fieldMap = ['id', 'title', 'thumb'];
            $videos = $this->lists_format($videos, $fieldMap, false);
            S($cache_key, $videos, '10');
        }
        
        $this->ajaxReturn(['state' => 0, 'data' => $videos]);
    }
}